package cn.iocoder.yudao.module.handguard.controller.admin.productaccess;

import cn.iocoder.yudao.module.handguard.dal.dataobject.usertablesaudit.UserTablesAuditDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.housingresource.HousingResourceMapper;
import cn.iocoder.yudao.module.handguard.service.wealthrecord.WealthRecordService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.productaccess.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.productaccess.ProductAccessDO;
import cn.iocoder.yudao.module.handguard.convert.productaccess.ProductAccessConvert;
import cn.iocoder.yudao.module.handguard.service.productaccess.ProductAccessService;

@Tag(name = "管理后台 - 产品准入")
@RestController
@RequestMapping("/hutai/product-access")
@Validated
public class ProductAccessController {

    @Resource
    private ProductAccessService productAccessService;

    @PostMapping("/create")
    @Operation(summary = "创建产品准入")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:create')")
    public CommonResult<Long> createProductAccess(@Valid @RequestBody ProductAccessCreateReqVO createReqVO) {
        return success(productAccessService.createProductAccess(createReqVO));
    }

    @PostMapping("/createCopyAccess")
    @Operation(summary = "创建复制产品准入")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:create')")
    public CommonResult<Long> createCopyAccess(@RequestParam("id") Long id) {
        return success(productAccessService.createCopyAccess(id));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品准入")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:update')")
    public CommonResult<Boolean> updateProductAccess(@Valid @RequestBody ProductAccessUpdateReqVO updateReqVO) {
        productAccessService.updateProductAccess(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品准入")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:product-access:delete')")
    public CommonResult<Boolean> deleteProductAccess(@RequestParam("id") Long id) {
        productAccessService.deleteProductAccess(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品准入")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:query')")
    public CommonResult<ProductAccessRespVO> getProductAccess(@RequestParam("id") Long id) {
        ProductAccessDO productAccess = productAccessService.getProductAccess(id);
        return success(ProductAccessConvert.INSTANCE.convert(productAccess));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品准入列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:query')")
    public CommonResult<List<ProductAccessRespVO>> getProductAccessList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductAccessDO> list = productAccessService.getProductAccessList(ids);
        return success(ProductAccessConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品准入分页")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:query')")
    public CommonResult<PageResult<ProductAccessRespVO>> getProductAccessPage(@Valid ProductAccessPageReqVO pageVO) {
        PageResult<ProductAccessDO> pageResult = productAccessService.getProductAccessPage(pageVO);
        return success(ProductAccessConvert.INSTANCE.convertPage(pageResult));
    }


    @GetMapping("/getConfigDataByKey")
    @Operation(summary = "根据key获得配置数据")
    @Parameter(name = "key", description = "key", required = true, example = "1024")
    public CommonResult<String> getConfigDataByKey(@RequestParam("key") String key) {
        String values = productAccessService.getConfigDataByKey(key);
        return success(values);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品准入 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:product-access:export')")
    @OperateLog(type = EXPORT)
    public void exportProductAccessExcel(@Valid ProductAccessExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductAccessDO> list = productAccessService.getProductAccessList(exportReqVO);
        // 导出 Excel
        List<ProductAccessExcelVO> datas = ProductAccessConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品准入.xls", "数据", ProductAccessExcelVO.class, datas);
    }

}
